﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaSuperGrid.SuperGrid.Model;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using RaSuperGrid.SuperGrid.Converter;

namespace RaSuperGrid.SuperGrid.ViewModel
{
    class PRItemViewModel : NotifyPropertyChanged, IEditableObject, IDataErrorInfo
    {
        private PR _model;
        private PR _oldValue;

        public PRItemViewModel(PR pr)
        {
            _model = pr;
        }

        public PR Model 
        { 
            get { return _model; } 
        }

        public DateTime ReqDate 
        {
            get { return _model.ReqDate; }
            set { if (value != _model.ReqDate) { _model.ReqDate = value; OnPropertyChanged("ReqDate"); } } 
        }
        public DateTime NeedDate 
        {
            get { return _model.NeedDate; } 
            set { if (value != _model.NeedDate) { _model.NeedDate = value; OnPropertyChanged("NeedDate"); } } 
        }
        public UrgencyType Urgency 
        {
            get { return _model.Urgency; } 
            set { if (value != _model.Urgency) { _model.Urgency = value; OnPropertyChanged("Urgency"); } } 
        }
        public int Reason 
        {
            get { return _model.Reason; } 
            set { if (value != _model.Reason) { _model.Reason = value; OnPropertyChanged("Reason"); } } 
        }
        public string ReqPerson 
        {
            get { return _model.ReqPerson; } 
            set { if (value != _model.ReqPerson) { _model.ReqPerson = value; OnPropertyChanged("ReqPerson"); } } 
        }
        public string Contact 
        {
            get { return _model.Contact; }
            set { if (value != _model.Contact) { _model.Contact = value; OnPropertyChanged("Contact"); } } 
        }
        public double Cost 
        { 
            get { return _model.Cost; } 
            set { if(value != _model.Cost) { _model.Cost = value; OnPropertyChanged("Cost"); } } 
        }
        public bool Repeatable 
        {
            get { return _model.Repeatable; }
            set { if (value != _model.Repeatable) { _model.Repeatable = value; OnPropertyChanged("Repeatable"); } } 
        }
        public string Description 
        {
            get { return _model.Description; }
            set { if (value != _model.Description) { _model.Description = value; OnPropertyChanged("Description"); } }
        }
        public string Special 
        {
            get { return _model.Special; }
            set { if (value != _model.Special) { _model.Special = value; OnPropertyChanged("Special"); } } 
        }

        public void BeginEdit()
        {
            _oldValue = _model.MakeCopy();
        }

        public void CancelEdit()
        {
            if (_oldValue != null)
            {
                _model = _oldValue;
                _oldValue = null;

                OnPropertyChanged("ReqDate");
                OnPropertyChanged("NeedDate");
                OnPropertyChanged("Urgency");
                OnPropertyChanged("Reason");
                OnPropertyChanged("ReqPerson");
                OnPropertyChanged("Contact");
                OnPropertyChanged("Cost");
                OnPropertyChanged("Repeatable");
                OnPropertyChanged("Description");
                OnPropertyChanged("Special");
            }
        }

        public void EndEdit()
        {
            _oldValue = null;
        }

        public string Error
        {
            get 
            {
                if (ReqDate >= NeedDate)
                    return "Need Date cannot be earlier than request Date.";
                else
                    return "";
            }
        }

        public string this[string columnName]
        {
            get 
            {
                switch (columnName)
                {
                    case "ReqDate":
                    case "NeedDate":
                        if (ReqDate >= NeedDate)
                            return "Need Date cannot be earlier than request Date.";
                        break;
                    case "Urgency":
                        break;
                    case "Reason":
                        break;
                    case "ReqPerson":
                        if (string.IsNullOrEmpty(ReqPerson))
                            return "Request Name cannot be empty";
                        break;
                    case "Contact":
                        if (!ContactValueConverter.IsValidContact(Contact))
                            return "Invalid Contact Number";
                        break;
                    case "Cost":
                        if (!CostValueConverter.IsValidCost(Cost))
                            return "Cost out of range";
                        break;
                    case "Repeatable":
                        break;
                    case "Description":
                        if (string.IsNullOrEmpty(Description))
                            return "Description cannot be empty";
                        break;
                    case "Special":
                        break;
                }

                return "";
            }
        }
    }
}
